//
//  OrderCommonCell.m
//  CarManager
//
//  Created by 王森林 on 2019/1/22.
//  Copyright © 2019年 王森林. All rights reserved.
//

#import "OrderCommonCell.h"
#import "ShopInfoOtherViewController.h"
#import "Car_CarDetailViewController.h"
@interface OrderCommonCell()
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *centerView;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel  *typeLabel;
@property (nonatomic, strong) UILabel  *typeLabel1;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIButton *statusBtn;
@property (nonatomic, strong) UIButton *cancleBtn;
@property (nonatomic, strong) NSDictionary *orderDict;
@end

@implementation OrderCommonCell


- (void)createUIWithData
{
    
    self.topView  = nil;
    self.centerView = nil;
    self.bottomView  = nil;
    [self.contentView addSubview:self.topView];
    self.topView.frame = CGRectMake(15, 15, BOUNDWIDTH - 30, 35);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)];
    [self.topView addGestureRecognizer:tap];
    [self createTopView];
    
    
    [self.contentView addSubview:self.centerView];
    self.centerView.frame = CGRectMake(0, 45, BOUNDWIDTH - 30, 120);
    [self createCenterView];
    
    
    [self.contentView addSubview:self.bottomView];
//    self.bottomView.frame = CGRectMake(15, 165, BOUNDWIDTH - 30, 100);
    [self createBottomView];
    
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(165);
        make.width.mas_equalTo(BOUNDWIDTH-30);
        make.height.mas_equalTo(110);
        make.left.mas_equalTo(self.contentView.mas_left).offset(15);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    

}

- (void)setData:(id)data
{
    self.orderDict = [NSDictionary dictionaryWithDictionary:data];
    [self createUIWithData];
    
}

- (void)handleTapAction:(UIGestureRecognizer *)gesture
{
    ShopInfoOtherViewController *controller = [[ShopInfoOtherViewController alloc] init];
    if (self.type.intValue == 1) {
          controller.TRADERS_ID = [self.orderDict objectForKey:@"TRADERS_ID"];
    }
    if (self.type.intValue == 2) {
        controller.TRADERS_ID = [self.orderDict objectForKey:@"JXSID0"];
    }
    if (self.type.intValue == 3) {
        controller.TRADERS_ID = [self.orderDict objectForKey:@"JXSID1"];
    }
  
    [[self viewController].navigationController pushViewController:controller animated:YES];
    
    
    
}

- (void)handlePayAction:(id)sender
{
    
    if (self.resultBlock) {
        self.resultBlock([self.orderDict objectForKey:@"ORDERINFO_ID"]);
    }
    
    
    
}

- (void)handleCancleAction:(id)sender
{
    
    if (self.cancleBlock) {
        self.cancleBlock([self.orderDict objectForKey:@"ORDERINFO_ID"]);
    }
    
    
    
}





- (void)createBottomView
{
    [self.bottomView removeAllSubviews];
    for  (int  i = 0 ; i<4; i++) {
        UIView *functionBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 10+23*i, (BOUNDWIDTH-30)/2, 15)];
        [self.bottomView addSubview:functionBGView];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,(15-4)/2 , 4, 4)];
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
        label.backgroundColor =  RGB(102, 102,102);
        [functionBGView addSubview:label];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(19, 0, (BOUNDWIDTH-30)/2-20, 15)];
        titleLabel.textColor = RGB(102, 102, 102);
        titleLabel.font = FONT_SIZE(14);
        [functionBGView addSubview:titleLabel];
        
        switch (i) {
            case 0:
            {
                
                if ([self.orderDict.allKeys containsObject:@"TCDZ00"]) {
                    titleLabel.text = [NSString stringWithFormat:@"提车地址: %@",[self.orderDict  objectForKey:@"TCDZ00"]];
                } else {
                    
                    titleLabel.text = [NSString stringWithFormat:@"提车地址: %@",@""];
                }
//                titleLabel.text = [NSString stringWithFormat:@"提车地址:%@",[self.orderDict objectForKey:@"TCDZ00"]];
                
            }
                break;
            case 1:
            {
                
//                titleLabel.text = [NSString stringWithFormat:@"联系人:%@",[self.orderDict objectForKey:@"LXR000"]];
                if ([self.orderDict.allKeys containsObject:@"LXR000"]) {
                    titleLabel.text = [NSString stringWithFormat:@"联系人: %@",[self.orderDict objectForKey:@"LXR000"]];
                } else {
                    
                    titleLabel.text = [NSString stringWithFormat:@"联系人: %@",@""];
                }
                
            }
                break;
            case 2:
            {
                if ([self.orderDict.allKeys containsObject:@"LXDH00"]) {
                    titleLabel.text = [NSString stringWithFormat:@"联系电话: %@",[ self.orderDict objectForKey:@"LXDH00"]];
                    
                } else {
                    
                    titleLabel.text = [NSString stringWithFormat:@"联系电话: %@",@""];
                    
                }
//                titleLabel.text = [NSString stringWithFormat:@"联系电话:%@",[self.orderDict objectForKey:@"LXDH00"]];
                //                titleLabel.text = [NSString stringWithFormat:@"联系人:%@",[self.dict objectForKey:@"LXR000"]];
                
            }
                break;
            case 3:
            {
                titleLabel.text = [NSString stringWithFormat:@"查看物流: %@",@""];
                [self.expanBtn removeFromSuperview];
                self.expanBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
                self.expanBtn.frame = CGRectMake(85, 0, 16, 16);
                [self.expanBtn setImage:IMG(@"xiala_default") forState:UIControlStateNormal];
                [self.expanBtn setImage:IMG(@"xiala_height") forState:UIControlStateSelected];
                [self.expanBtn addTarget:self action:@selector(handleExpanAction:) forControlEvents:UIControlEventTouchUpInside];
                [functionBGView addSubview:self.expanBtn];
                
            }
                break;
                
            default:
                break;
        }
        
        
    }
    
}

-  (void)handleExpanAction:(id)sender
{
    UIButton * btn = sender;
    if (!btn.selected) {
        btn.selected = YES;
        if (self.expanBlock) {
            self.expanBlock(@"1");
        }
//        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(self.contentView.mas_top).offset(165);
//            make.width.mas_equalTo(BOUNDWIDTH-30);
//            make.height.mas_equalTo(100);
//            make.left.mas_equalTo(self.contentView.mas_left).offset(15);
//            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-50);
//        }];
//         [self.bottomView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    } else {
        btn.selected = NO;
        if (self.expanBlock) {
            self.expanBlock(@"2");
        }
//        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//            
//            make.top.mas_equalTo(self.contentView.mas_top).offset(165);
//            make.width.mas_equalTo(BOUNDWIDTH-30);
//            make.height.mas_equalTo(100);
//            make.left.mas_equalTo(self.contentView.mas_left).offset(15);
//            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-10);
//        }];
//         [self.bottomView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    }
  
}

- (void)handleDetailAction:(UITapGestureRecognizer *)gesture
{
    Car_CarDetailViewController *push = [[Car_CarDetailViewController alloc]init];
    //    NSDictionary  *dict = self.carHotAry[indexPath.row - 5];
    push.SERIES_ID = [self.orderDict objectForKey:@"ZSCXID"];
    [[self  viewController].navigationController pushViewController:push animated:YES];
    
}

- (void)createCenterView
{
    
    [self.centerView addSubview:self.iconImageView];
    [self.centerView addSubview:self.titleLabel];
    [self.centerView addSubview:self.typeLabel];
    [self.centerView addSubview:self.typeLabel1];
    [self.centerView addSubview:self.statusBtn];
     [self.centerView addSubview:self.cancleBtn];
    //    [self.contentView addSubview:self.collectLabel];
    //    [self.contentView addSubview:self.collectImageView];
    [self.centerView addSubview:self.priceLabel];
    
//    [self.iconImageView setImage:IMG(@"car_test1")];
    
    
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(handleDetailAction:)];
    [self.centerView addGestureRecognizer:tap];
//    self.iconImageView.userInteractionEnabled = YES;
//    [self.iconImageView  addGestureRecognizer:tap];
//    [self.titleLabel addGestureRecognizer:tap];
    
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL,[self.orderDict objectForKey:@"ZST000"])]];
     if (self.type.integerValue == 1) {
        self.typeLabel.hidden = NO;
        self.typeLabel1.hidden = NO;
        self.titleLabel.text = [self.orderDict objectForKey:@"CXMC00"];
        self.typeLabel.text = [self.orderDict objectForKey:@"GHFSMC"];
        self.typeLabel1.text = [self.orderDict objectForKey:@"RLMC00"];
        self.priceLabel.text = [NSString stringWithFormat:@"订金:¥%@元",[self.orderDict objectForKey:@"DJ0000"]];
//        self.statusLabel.text = [self.orderDict objectForKey:@"DDZT00"];
    } else {
        self.typeLabel.hidden = YES;
          self.typeLabel1.hidden = YES;
        self.titleLabel.text = [self.orderDict objectForKey:@"ZSCXMC"];
//        self.typeLabel.text = [self.orderDict objectForKey:@"GHFSMC"];
        self.priceLabel.text = [NSString stringWithFormat:@"订金:¥%@元",[self.orderDict objectForKey:@"YFJE00"]];
//        self.statusLabel.text = [self.orderDict objectForKey:@"DDZT00"];
    }

    NSInteger status = [[self.orderDict objectForKey:@"DDZT00"] integerValue];
    switch (status) {
        case 0:
        {
            //定金已经支付
            [self.statusBtn setTitle:@"已取消" forState:UIControlStateNormal];
            [self.statusBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
            self.statusBtn.backgroundColor = [UIColor clearColor];
            self.statusBtn.layer.cornerRadius = 0;
            self.statusBtn.layer.masksToBounds = YES;
            self.cancleBtn.hidden = YES;
        }
            break;
        case 1:
        {
            //定金已经支付
             [self.statusBtn setTitle:@"已支付" forState:UIControlStateNormal];
            [self.statusBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
            self.statusBtn.backgroundColor = [UIColor clearColor];
            self.statusBtn.layer.cornerRadius = 0;
            self.statusBtn.layer.masksToBounds = YES;
             self.cancleBtn.hidden = YES;
        }
            break;
        case 2:
        {
            //2：显示黄色按钮”去支付“
            [self.statusBtn setTitle:@"去支付" forState:UIControlStateNormal];
            [self.statusBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.statusBtn.backgroundColor = RGB(255, 218, 68);
            self.statusBtn.layer.cornerRadius = 7;
            self.statusBtn.layer.masksToBounds = YES;
            [self.statusBtn addTarget:self action:@selector(handlePayAction:) forControlEvents:UIControlEventTouchUpInside];
            
            self.cancleBtn.hidden = NO;
            [self.cancleBtn setTitle:@"取消订单" forState:UIControlStateNormal];
            [self.cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            self.cancleBtn.backgroundColor = RGB(255, 218, 68);
            self.cancleBtn.layer.cornerRadius = 7;
            self.cancleBtn.layer.masksToBounds = YES;
            [self.cancleBtn addTarget:self action:@selector(handleCancleAction:) forControlEvents:UIControlEventTouchUpInside];
            
        }
            break;
        case 9:
        {
            //9：订单已完成
            [self.statusBtn setTitle:@"已完成" forState:UIControlStateNormal];
            [self.statusBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
            self.statusBtn.backgroundColor = [UIColor clearColor];
            self.statusBtn.layer.cornerRadius = 0;
            self.statusBtn.layer.masksToBounds = YES;
              self.cancleBtn.hidden = YES;
        }
            break;
            
        default:
        {
            //其他：订单异常
            [self.statusBtn setTitleColor:Common_TextColor forState:UIControlStateNormal];
            self.statusBtn.backgroundColor = [UIColor clearColor];
            self.statusBtn.layer.cornerRadius = 0;
            self.statusBtn.layer.masksToBounds = YES;
            
            [self.statusBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [self.statusBtn setTitle:@"订单异常" forState:UIControlStateNormal];
              self.cancleBtn.hidden = YES;
        }
            break;
    }
    
    
    
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.centerView.mas_top).offset(17);
        make.left.equalTo(self.centerView.mas_left).offset(15);
        make.width.mas_equalTo(151);
        make.height.mas_equalTo(92);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.centerView.mas_top).offset(17);
        make.left.equalTo(self.iconImageView.mas_right).offset(6);
        make.right.equalTo(self.centerView.mas_right).offset(10);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.centerView.mas_bottom).offset(-15);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(18);
        make.left.equalTo(self.iconImageView.mas_right).offset(6);
    }];
    
    [self.statusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.centerView.mas_bottom).offset(-15);
        make.right.equalTo(self.centerView.mas_right).offset(10);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(70);
//        make.left.equalTo(self.priceLabel.mas_right).offset(6);
    }];
    
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.statusBtn.mas_top).offset(-10);
        make.right.equalTo(self.centerView.mas_right).offset(10);
        make.height.mas_equalTo(18);
        make.width.mas_equalTo(70);
        //        make.left.equalTo(self.priceLabel.mas_right).offset(6);
    }];
    
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.priceLabel.mas_top).offset(-11);
        make.left.equalTo(self.iconImageView.mas_right).offset(6);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(18);
    }];
    
    [self.typeLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.priceLabel.mas_top).offset(-11);
        make.left.equalTo(self.typeLabel.mas_right).offset(10);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(18);
    }];
    
}

- (void)createTopView
{
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 15, 15)];
    imageView1.image = IMG(@"cardetail_1");
    [self.topView addSubview:imageView1];
    
    
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 10, 15, 15)];
    imageView2.image = IMG(@"cardetail_2");
    [self.topView  addSubview:imageView2];
    
    
    
    UILabel  *companyLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, BOUNDWIDTH - 80, 15)];
    companyLabel.textColor = RGB(242, 242,242);
    companyLabel.font = FONT_SIZE(12);
    //    companyLabel.text = [infoDict objectForKey:@"JXSMC0"];
    companyLabel.text = [self.orderDict objectForKey:@"JXSMC1"];
    
    if (self.type.intValue == 1) {
           companyLabel.text = [self.orderDict objectForKey:@"JXSMC0"];
    }
    if (self.type.intValue == 2) {
        companyLabel.text = [self.orderDict objectForKey:@"JXSMC0"];
    }
    
    if (self.type.intValue == 3) {
          companyLabel.text = [self.orderDict objectForKey:@"JXSMC1"];
    }
    [self.topView  addSubview:companyLabel];
    
}

- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView new];
        _topView.backgroundColor = Common_HeadTitle_Color;
    }
    return _topView;
    
}

- (UIView *)centerView
{
    if (!_centerView) {
        _centerView = [UIView new];
    }
    return _centerView;
    
}
- (UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [UIView new];
        _bottomView.backgroundColor = Common_BackgroundColor;
        _bottomView.layer.cornerRadius = 10;
        _bottomView.layer.masksToBounds = YES;
    }
    return _bottomView;
    
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        
    }
    return _iconImageView;
}


- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = RGB(40, 40, 40);
        _titleLabel.font = FONT_SIZE(14);
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
    
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [UILabel new];
        _typeLabel.textColor = Common_Text_TextfieldColor;
        _typeLabel.textAlignment = NSTextAlignmentCenter;
        _typeLabel.font = FONT_SIZE(12);
        _typeLabel.layer.borderWidth = 1;
        _typeLabel.layer.cornerRadius = 6;
        _typeLabel.layer.masksToBounds = YES;
        _typeLabel.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel;
    
}
- (UILabel *)typeLabel1
{
    if (!_typeLabel1) {
        _typeLabel1 = [UILabel new];
        _typeLabel1.textColor = Common_Text_TextfieldColor;
        _typeLabel1.textAlignment = NSTextAlignmentCenter;
        _typeLabel1.font = FONT_SIZE(12);
        _typeLabel1.layer.borderWidth = 1;
        _typeLabel1.layer.cornerRadius = 6;
        _typeLabel1.layer.masksToBounds = YES;
        _typeLabel1.layer.borderColor = Common_Head_Color.CGColor;
    }
    return _typeLabel1;
    
}
- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel new];
        _priceLabel.textColor = RGB(51, 51, 51);
        _priceLabel.textAlignment = NSTextAlignmentLeft;
        _priceLabel.font = B_FONT_SIZE(14);
        
    }
    return _priceLabel;
    
}

- (UIButton *)statusBtn
{
    if (!_statusBtn) {
        _statusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _statusBtn.textColor = Common_TextColor;
        [_statusBtn setTitleColor:Common_NavigroundColor forState:UIControlStateNormal];
        _statusBtn.titleLabel.font = FONT_SIZE(12);
//       _statusBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
      
        
    }
    return _statusBtn;
    
}


- (UIButton *)cancleBtn
{
    if (!_cancleBtn) {
        _cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        //        _statusBtn.textColor = Common_TextColor;
        [_cancleBtn setTitleColor:Common_NavigroundColor forState:UIControlStateNormal];
        _cancleBtn.titleLabel.font = FONT_SIZE(12);
        //       _statusBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        
        
    }
    return _cancleBtn;
    
}



@end
